﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Wixa.Util.Helper
{
    public static class AssemblyHelper
    {
        /// <summary>
        /// 解决方案程序集匹配名
        /// </summary>
        public const string FXASSEMBLY_PATTERN = "Wixa";

        public static List<Type> GetAllTypes()
        {
            var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var allFxAssemblies = Directory.GetFiles(rootPath, "*.dll")
                .Where(x => new FileInfo(x).Name.Contains(FXASSEMBLY_PATTERN))
                .Select(x => Assembly.LoadFrom(x))
                .Where(x => !x.IsDynamic)
                .ToList();
            var allFxTypes = new List<Type>();
            allFxAssemblies.ForEach(x =>
            {
                try
                {
                    allFxTypes.AddRange(x.GetTypes());
                }
                catch (Exception)
                {

                }
            });
            return allFxTypes;
        }
    }
}
